package model.game;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import model.Server;

public class World implements Serializable {
    private Map map = new Map();
    //private ArrayList<Scenario> scenarios = new ArrayList<Scenario>();

    public World(Server server) {
        try {
            World world = load();
            this.map = world.getMap();
            //this.players = world.getPlayers();
            server.sendToConsole("Carregando mundo...");
        } catch (IOException e) {
            server.sendToConsole("Criando novo mundo...");
            //scenarios.add(new JunyeCity());
            
            save();
        } catch (ClassNotFoundException e) {
        } catch (Exception e) {
        }
    }

    public void save() {
        ObjectOutputStream oos;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("world.sav"));
            oos.writeObject(this);
            oos.close();
        } catch (IOException ex) {
            //Erro//
        }
    }

    private static World load() throws IOException, ClassNotFoundException {
        World worldLoaded = null;
        ObjectInputStream ois =
            new ObjectInputStream(new FileInputStream("world.sav"));
        worldLoaded = (World) ois.readObject();
        ois.close();
        return worldLoaded;
    } 

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }
}
